﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var data = new List<byte[]>();
            const int frameCount = 24;
            const int channelsCount = 16;
            const int bytesPerChannel = 3;
            const byte header = 0xaa;
            for (int i = 0; i < frameCount; i++)
            {
                var row = new byte[channelsCount*bytesPerChannel + 1];
                var rnd = new Random();
                rnd.NextBytes(row);
                row[0] = header;
                data.Add(row);
            }

            var streams = new List<MemoryStream>();
            for (int i = 0; i < channelsCount; i++)
            {
                streams.Add(new MemoryStream());
            }

            var buffer = data.SelectMany(ba => ba).ToArray();

            var msg = string.Empty;
            var valid = SoundSampleReceiver.ViewModels.MainWindowViewModel.IsValidPacket(buffer, out msg);
            Assert.IsTrue(valid && (msg == null));

            SoundSampleReceiver.ViewModels.MainWindowViewModel.WriteBytes(new List<Stream>(streams), buffer);

            for (int i = 0; i < channelsCount; i++)
            {
                var row = new List<byte>();
                for (int j = 0; j < frameCount; j++)
                {
                    row.Add(data[j][i*bytesPerChannel + 1]);
                    row.Add(data[j][i * bytesPerChannel + 2]);
                    row.Add(data[j][i * bytesPerChannel + 3]);
                }

                var memStream = (MemoryStream) streams[i];
                Assert.IsTrue(row.ToArray().SequenceEqual(memStream.ToArray()));
            }
        }
    }
}
